import { defineStore } from 'pinia';
import config from '@/config/index.js';

/**
 * useStroe 可以是任意命名
 */

import Cookies from 'js-cookie';

const cookiesStorage = {
    // key my_cookie
    // value json
	setItem(key, val) {
		return Cookies.set(key,val, { expires: config.cookie_expire });
	},
	getItem(key) {
		return Cookies.get(key);
	},
	// removeItem(key) {},
	// clear() {},
	// key(key) {},
	// length
};

export default   defineStore(
    'my_cookie',

    ()=>{
        let hiddenAdvertise = ref(false);
		let activeIndex = ref(0);
		let isLogin=ref(false)
		let userId=ref(null)
		let recommendId=ref(null)
		let recommendTime=ref(null)
		
		function setIsLogin(v) {
			isLogin.value = v;
		}
		function setHiddenAdvertise(v) {
			hiddenAdvertise.value = v;
		}
		function setActiveIndex(index) {
			activeIndex.value = index;
		}
		function setUserId(v) {
			userId.value = v;
		}
		function clear(){
			isLogin.value=false,
			userId.value=null
		}
		function setRecommendId(val){
			recommendId.value=val
		}
		function setRecommendTime(val){
			recommendTime.value=val;
		}
      
        return {
            hiddenAdvertise,
			activeIndex,
			isLogin,
			userId,
			recommendId,
			recommendTime,
			
			setHiddenAdvertise,
			setRecommendTime,
			setRecommendId,
			setActiveIndex,
			setIsLogin,
			setUserId,
			clear
        }
    },
    {
		persist: {
			enabled: true,
			storage: cookiesStorage,
			key: 'my_cookie'
		}
	}
)
